package com.zhtd.knocker.util;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import java.lang.reflect.Type;

/**
 * 使用gson 包操作json 注意：要求实体的字段有get 和set
 * 
 * @author Administrator
 *
 */
public class GsonUtils {
	private static Gson mHandler = new Gson();

	public static String toJSON(Object src) {
		try {
			return mHandler.toJson(src);
		} catch (Exception e) {
			return null;
		}
	}
	
	public static String toJSON(Object src,Type classOfT ) {
		try {
			return mHandler.toJson(src,classOfT);
		} catch (Exception e) {
			return null;
		}
	}

	public static <T> T fromJson(String json, Class<T> classOfT) {
		try {
			return mHandler.fromJson(json, classOfT);
		} catch (JsonSyntaxException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static <T> T fromJson(String json, Type typeOfT) {
		try {
			return mHandler.fromJson(json, typeOfT);
		} catch (JsonSyntaxException e) {
			e.printStackTrace();
			if (e.getMessage() != null) {
				Log.d("gson",e.getMessage());
			}
			return null;
		}
	}
}
